<?php
/**
 * @author Erling Owe <erling.owe@gmail.com>
 * @package Core
 * @subpackage Data
 */


class Core_Data_SQL_Structure {

    private $arrDatabases = array ();

    function __construct () {

    }

    public function info () {

        $objHTML = new Core_Presentation_HTML_Document ();
        $objHTML->Title = "Core_Data_SQL_Structure::info()";

        $objHTML->addStyle( "*", "font-family", '"Helvetica", "Verdana"' );
        $objHTML->addStyle( "*", "color", "white" );
        $objHTML->addStyle( "body", "background-color", "black" );
        $objHTML->addStyle( "th, td", "font-size", "9pt" );

        $strHTML = "";
        $strHTML .= "<h1>Core_Data_SQL_Structure::info()</h1>";
        $strHTML .= "<h2>Databases</h2>";

        foreach ( $this->databases() as $objDatabase ) {

            $strHTML .= "<h3>" . $objDatabase . " @ " . $objDatabase->Connection->ServerInformation->Host . "</h3>";

            foreach ( $objDatabase->tables() as $objTable ) {

                $strHTML .= "<h4>" . $objTable . "</h4>";
                $strHTML .= "<table>";
                $strHTML .= "<tr><th>Name</th><th>Data Type</th><th>Required</th><th>Key Type</th><th>Auto Increment</th></tr>";

                foreach ( $objTable->fields() as $objField ) {

                    $strHTML .= "<tr>";
                    $strHTML .= "<td>" . $objField . "</td>";
                    $strHTML .= "<td>" . Core_Data_SQL_DataTypes::getQueryText( $objField->getDataType(), $objDatabase->Connection->ServerInformation->Type );
                    if ( $objField->getLength() ) {
                        $strHTML .= "(" . $objField->getLength() . ")";
                    }
                    $strHTML .= "</td>";
                    $strHTML .= "<td>";
                    $strHTML .= ( $objField->isRequired() ) ? "true" : "false";
                    $strHTML .= "</td>";

                    $strHTML .= "<td>" . Core_Data_SQL_KeyTypes::getQueryText( $objField->getKeyType(), $objDatabase->Connection->ServerInformation->Type ) . "</td>";

                    $strHTML .= "<td>";
                    $strHTML .= ( $objField->autoIncrement() ) ? "true" : "false";
                    $strHTML .= "</td>";

                    $strHTML .= "</tr>";

                }

                $strHTML .= "</table>";

            }

        }

        $objHTML->importHTML( $strHTML );

        echo $objHTML->getHTML();

    }

    public function addDatabase ( Core_Data_SQL_Connection $Connection, $strDatabase = null ) {
        $strDatabase = ( $strDatabase ) ? $strDatabase : $Connection->getCurrentDatabase();
        $objDatabase = new Core_Data_SQL_Structure_Database ( $Connection, $strDatabase );
        $this->arrDatabases[(string) $objDatabase] = $objDatabase;
        return $objDatabase;
    }

    public function databases () {
        return (object) $this->arrDatabases;
    }

    public function createTable () {

    }

    public function createField () {

    }

}

?>